package com.yjhb.android.sbz.v2.adapter

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.v2.entity.FissionRewardItem
import com.yjhb.android.sbz.view.drawable.NumberDrawable
import com.jojo.android.zxlib.base.BaseRecyclerAdapterV2
import java.text.NumberFormat


/**
 *  创建者 zx
 *  创建时间 2021/4/7 17:48
 */
class V2FissionRewardAdapter(context: Context?) : BaseRecyclerAdapterV2<BaseRecyclerAdapterV2.BaseHolder, FissionRewardItem>(context) {
    /**
     * 当前邀请人数
     */
    var currNum = 0

    /**
     * 当前已领取的阶段
     */
    var currentStage = 0

    override fun getLayoutId(): Int {
        return R.layout.item_fission_reward
    }

    override fun onBindViewHolder(holder: BaseHolder, position: Int) {
        val numTv = holder.findViewById<View>(R.id.num_tv)
        if (numTv.background == null || numTv.background !is NumberDrawable) {
            val numberDrawable = NumberDrawable()
            numberDrawable.bgColor = Color.parseColor("#FF4040")
            numberDrawable.textColor = Color.WHITE
            numTv.background = numberDrawable
        }
        (numTv.background as NumberDrawable).number = entity.stageNum
        holder.findViewById<TextView>(R.id.content_tv).text = "一级好友提现${entity.maxNum}人"
        holder.findViewById<TextView>(R.id.money_tv).text = "${entity.reward}元"
        val progressBar = holder.findViewById<ProgressBar>(R.id.progressbar)
        progressBar.max = entity.maxNum
        progressBar.progress = currNum
        val numberFormat = NumberFormat.getInstance()
        numberFormat.maximumFractionDigits = 2
        val i = currNum.toDouble() / entity.maxNum.toDouble() * 100
        holder.findViewById<TextView>(R.id.progress_tv).text = numberFormat.format(if (i >= 100) 100 else i) + "%"
        val button = holder.findViewById<TextView>(R.id.button_tv)
        button.setOnClickListener {
            itemClickListener?.onViewClick(it, "receive", position, data[position])
        }
        if (currentStage >= entity.stageNum) {
            button.isEnabled = false
            button.textSize = 9f
            button.text = "已领取"
        } else {
            button.isEnabled = true
            button.textSize = 12f
            button.text = "领取"
        }
    }

    override fun getHolder(v: View): BaseHolder {
        return BaseHolder(v)
    }


}